/*
 * VentanaNuevoPerfil.java
 *
 * Created on 18 de septiembre de 2008, 09:13
 */

package vista;

import dominio.Perfil;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;
import vista.util.VentanaNuevoPerfilUtil;

/**
 *
 * @author  marcelo
 */
public class VentanaNuevoPerfil extends javax.swing.JFrame
{

    private VentanaNuevoPerfilUtil util = new VentanaNuevoPerfilUtil();
    private Collection temas = new ArrayList();
    private Collection canales = new ArrayList();
    private Collection estados = new ArrayList();
    private Collection recetas = new ArrayList();
    private Collection perfiles = new ArrayList();

    private boolean guardar = false;

    /** Creates new form VentanaNuevoPerfil */
    public VentanaNuevoPerfil() {
        initComponents();
        this.inicializar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonNuevoCanal = new javax.swing.JButton();
        jButtonNuevoEstiloMusica = new javax.swing.JButton();
        jButtonNuevaReceta = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableCanales = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableMusica = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTableRecetas = new javax.swing.JTable();
        jLabelCantidad3 = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jComboBoxCaritas = new javax.swing.JComboBox();
        jLabelCarita = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jSliderTemperatura = new javax.swing.JSlider();
        jButtonGuardar = new javax.swing.JButton();
        jButtonSalir = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablePerfiles = new javax.swing.JTable();
        jButtonNuevo = new javax.swing.JButton();
        jButtonModificar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nuevo Perfil");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Valores"));

        jButtonNuevoCanal.setText("Agregar Canal");
        jButtonNuevoCanal.setEnabled(false);
        jButtonNuevoCanal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoCanalActionPerformed(evt);
            }
        });

        jButtonNuevoEstiloMusica.setText("Agregar Temas");
        jButtonNuevoEstiloMusica.setEnabled(false);
        jButtonNuevoEstiloMusica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoEstiloMusicaActionPerformed(evt);
            }
        });

        jButtonNuevaReceta.setText("Agregar Receta");
        jButtonNuevaReceta.setEnabled(false);
        jButtonNuevaReceta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevaRecetaActionPerformed(evt);
            }
        });

        jTableCanales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null}
            },
            new String [] {
                "Canales"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTableCanales);

        jTableMusica.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null}
            },
            new String [] {
                "Estilo de Musica"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jTableMusica);

        jTableRecetas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null}
            },
            new String [] {
                "Recetas"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(jTableRecetas);

        jLabelCantidad3.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad3.setText("Nombre");
        jLabelCantidad3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad3.setOpaque(true);

        jTextFieldNombre.setEnabled(false);

        jComboBoxCaritas.setBorder(null);
        jComboBoxCaritas.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonNuevoCanal, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(61, 61, 61)
                .addComponent(jButtonNuevoEstiloMusica, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 136, Short.MAX_VALUE)
                .addComponent(jButtonNuevaReceta, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(69, 69, 69))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelCantidad3, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jComboBoxCaritas, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabelCarita, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonNuevoEstiloMusica)
                        .addComponent(jButtonNuevoCanal))
                    .addComponent(jButtonNuevaReceta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBoxCaritas, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabelCantidad3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelCarita, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Intensidad de la Luz"));

        jSliderTemperatura.setMajorTickSpacing(5);
        jSliderTemperatura.setMinorTickSpacing(1);
        jSliderTemperatura.setPaintLabels(true);
        jSliderTemperatura.setPaintTicks(true);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSliderTemperatura, javax.swing.GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jSliderTemperatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jButtonGuardar.setText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonSalir.setText("Salir");
        jButtonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalirActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Perfiles"));

        jTablePerfiles.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Codigo", "Nombre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTablePerfiles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTablePerfilesMouseClicked(evt);
            }
        });
        jTablePerfiles.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTablePerfilesKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTablePerfiles);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
        );

        jButtonNuevo.setText("Nuevo");
        jButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoActionPerformed(evt);
            }
        });

        jButtonModificar.setText("Modificar");
        jButtonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonModificarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(168, 168, 168)
                        .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonModificar)
                    .addComponent(jButtonNuevo)
                    .addComponent(jButtonGuardar)
                    .addComponent(jButtonSalir))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalirActionPerformed
// TODO add your handling code here:
//    this.getCarita();
    this.dispose();
}//GEN-LAST:event_jButtonSalirActionPerformed

private void jButtonNuevoCanalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoCanalActionPerformed
// TODO add your handling code here:
    VentanaAgregarCanal agregarCanal = new VentanaAgregarCanal(this);
    agregarCanal.setVisible(true);
}//GEN-LAST:event_jButtonNuevoCanalActionPerformed

private void jButtonNuevoEstiloMusicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoEstiloMusicaActionPerformed

    VentanaAgregarTema agregar = new VentanaAgregarTema(this);
    agregar.setVisible(true);
}//GEN-LAST:event_jButtonNuevoEstiloMusicaActionPerformed

private void jButtonNuevaRecetaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevaRecetaActionPerformed
// TODO add your handling code here:
    VentanaNuevaReceta nuevaReceta = new VentanaNuevaReceta(this);
    nuevaReceta.setVisible(true);
}//GEN-LAST:event_jButtonNuevaRecetaActionPerformed

private void jButtonNuevoActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonNuevoActionPerformed
{//GEN-HEADEREND:event_jButtonNuevoActionPerformed
    // TODO add your handling code here:
    this.guardar = true;
    this.limpiarTodo();
    this.habilitar();
    this.deshabilitarBotones();
    this.jButtonGuardar.setEnabled(true);
}//GEN-LAST:event_jButtonNuevoActionPerformed

private void jButtonModificarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonModificarActionPerformed
{//GEN-HEADEREND:event_jButtonModificarActionPerformed
    // TODO add your handling code here:
    this.guardar = false;
    this.habilitar();
    this.deshabilitarBotones();
    this.jButtonGuardar.setEnabled(true);
}//GEN-LAST:event_jButtonModificarActionPerformed

private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonGuardarActionPerformed
{//GEN-HEADEREND:event_jButtonGuardarActionPerformed
    // TODO add your handling code here:
    if(this.guardar)
        this.guardar();
    else
        this.modificar();
    
}//GEN-LAST:event_jButtonGuardarActionPerformed

private void jTablePerfilesMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTablePerfilesMouseClicked
{//GEN-HEADEREND:event_jTablePerfilesMouseClicked
    // TODO add your handling code here:
    this.mostrarPerfil();
}//GEN-LAST:event_jTablePerfilesMouseClicked

private void jTablePerfilesKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_jTablePerfilesKeyPressed
{//GEN-HEADEREND:event_jTablePerfilesKeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode() == 127)
        this.borrar();
}//GEN-LAST:event_jTablePerfilesKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaNuevoPerfil().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonModificar;
    private javax.swing.JButton jButtonNuevaReceta;
    private javax.swing.JButton jButtonNuevo;
    private javax.swing.JButton jButtonNuevoCanal;
    private javax.swing.JButton jButtonNuevoEstiloMusica;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JComboBox jComboBoxCaritas;
    private javax.swing.JLabel jLabelCantidad3;
    private javax.swing.JLabel jLabelCarita;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSlider jSliderTemperatura;
    private javax.swing.JTable jTableCanales;
    private javax.swing.JTable jTableMusica;
    private javax.swing.JTable jTablePerfiles;
    private javax.swing.JTable jTableRecetas;
    private javax.swing.JTextField jTextFieldNombre;
    // End of variables declaration//GEN-END:variables

    private void inicializar()
    {
        this.centrar();
        this.ajustarTamanioColumna();
//        this.llenarTablaEstadosDeAnimo();
        this.llenarTablaPerfiles();
        this.llenarCaritas();
    }

    private void centrar()
    {
        Dimension pantalla, cuadro;
	pantalla = Toolkit.getDefaultToolkit().getScreenSize();
	cuadro = this.getSize();
	this.setLocation(((pantalla.width - cuadro.width)/2), (pantalla.height - cuadro.height)/2);
    }
    
    protected static ImageIcon createImageIcon(String path)
    {
        //FrmLogin es el nombre de la clase
        java.net.URL imgURL = VentanaNuevoPerfil.class.getResource(path);
//        System.out.println("Path:" + imgURL.getPath());
        if (imgURL != null)
            return new ImageIcon(imgURL);
        else
            return null;
    }

    private void llenarCaritas()
    {
        ImageIcon i = createImageIcon("/imagenes/SinEmocion.jpg");
        ImageIcon tmpIcon = new ImageIcon(i.getImage().getScaledInstance(this.jLabelCarita.getWidth(), this.jLabelCarita.getHeight(), Image.SCALE_DEFAULT));
        this.jComboBoxCaritas.addItem(tmpIcon);
        i = createImageIcon("/imagenes/Enfermo.jpg");
        tmpIcon = new ImageIcon(i.getImage().getScaledInstance(this.jLabelCarita.getWidth(), this.jLabelCarita.getHeight(), Image.SCALE_DEFAULT));
        this.jComboBoxCaritas.addItem(tmpIcon);
        i = createImageIcon("/imagenes/Enojado.jpg");
        tmpIcon = new ImageIcon(i.getImage().getScaledInstance(this.jLabelCarita.getWidth(), this.jLabelCarita.getHeight(), Image.SCALE_DEFAULT));
        this.jComboBoxCaritas.addItem(tmpIcon);
        i = createImageIcon("/imagenes/Feliz.jpg");
        tmpIcon = new ImageIcon(i.getImage().getScaledInstance(this.jLabelCarita.getWidth(), this.jLabelCarita.getHeight(), Image.SCALE_DEFAULT));
        this.jComboBoxCaritas.addItem(tmpIcon);
        i = createImageIcon("/imagenes/Triste.jpg");
        tmpIcon = new ImageIcon(i.getImage().getScaledInstance(this.jLabelCarita.getWidth(), this.jLabelCarita.getHeight(), Image.SCALE_DEFAULT));
        this.jComboBoxCaritas.addItem(tmpIcon);
    }
    
    private void ajustarTamanioColumna()
    {
        TableColumn column = null;
        for (int i = 0; i < 2; i++)
        {
            column = jTablePerfiles.getColumnModel().getColumn(i);
            switch(i)
            {
                case 0:
                        column.setPreferredWidth(15);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 1:
                        column.setPreferredWidth(257);
//                        System.out.println("El tamaño de la clumna 2 es: "+column.getPreferredWidth());
                        break;
            }
        }
//        for (int i = 0; i < 3; i++)
//        {
//            column = jTableEstadosAnimo.getColumnModel().getColumn(i);
//
//            switch(i)
//            {
//                case 0:
//                        column.setPreferredWidth(21);
////                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
//                        break;
//                case 1:
//                        column.setPreferredWidth(176);
////                        System.out.println("El tamaño de la clumna 2 es: "+column.getPreferredWidth());
//                        break;
//                case 2:
////                        System.out.println("El tamaño de la clumna 3 es: "+column.getPreferredWidth());
//                        column.setPreferredWidth(28);
//                        break;
//            }
//        }
    }

//    private void desmarcar()
//    {
//        boolean marca = Boolean.parseBoolean(String.valueOf(this.jTableEstadosAnimo.getValueAt(this.jTableEstadosAnimo.getSelectedRow(), 2)));
//        this.util.desmarcar(this.jTableEstadosAnimo, marca, this.jTableEstadosAnimo.getSelectedRow());
//    }

    private void llenarTablaMusica()
    {
        this.util.cargarTablaTemas(jTableMusica, temas);
    }

    private void llenarTablaCanales()
    {
        this.util.cargarTablaCanales(this.jTableCanales, canales);
    }

//    private void llenarTablaEstadosDeAnimo()
//    {
//        this.estados = this.util.cargarTablaEstadosDeAnimo(this.jTableEstadosAnimo, this.estados);
//    }

    private void llenarTablaPerfiles()
    {
        this.perfiles = this.util.cargarTablaPerfiles(this.jTablePerfiles);
    }

    private void llenarTablaRecetas()
    {
        this.util.cargarTablaRecetas(this.jTableRecetas, this.recetas);
    }

    public void setTemas(Collection temas)
    {
        this.temas = temas;
        this.llenarTablaMusica();
    }

    public void setCanales(Collection canales)
    {
        this.canales = canales;
        this.llenarTablaCanales();
    }

    public void setRecetas(Collection recetas)
    {
        this.recetas = recetas;
        this.llenarTablaRecetas();
    }

    private void limpiarTodo()
    {
        //limpiar las marcas de la tabla estados de animo
//        this.util.descmarcarTodo(this.jTableEstadosAnimo);
        //limpiar el campo nombre
        this.jTextFieldNombre.setText("");
        //limpiar las tablas musica, canales y recetas, con sus respectivas colecciones
        this.canales.clear();
        this.util.limpiar(this.jTableCanales);
        this.temas.clear();
        this.util.limpiar(jTableMusica);
        this.recetas.clear();
        this.util.limpiar(this.jTableRecetas);
        //poner el slider al medio
        this.jSliderTemperatura.setValue(50);
    }

    private void habilitar()
    {
        //habilitar botones
        this.jButtonNuevoCanal.setEnabled(true);
        this.jButtonNuevoEstiloMusica.setEnabled(true);
        this.jButtonNuevaReceta.setEnabled(true);
        //habilitar textbox
        this.jTextFieldNombre.setEnabled(true);
        //habilitar combo
        this.jComboBoxCaritas.setEnabled(true);
    }

    private void deshabilitar()
    {
        //habilitar botones
        this.jButtonNuevoCanal.setEnabled(false);
        this.jButtonNuevoEstiloMusica.setEnabled(false);
        this.jButtonNuevaReceta.setEnabled(false);
        //habilitar textbox
        this.jTextFieldNombre.setEnabled(false);
        //habilitar combo
        this.jComboBoxCaritas.setEnabled(false);
    }

    private void habilitarBotones()
    {
        this.jButtonModificar.setEnabled(true);
        this.jButtonNuevo.setEnabled(true);
    }

    private void deshabilitarBotones()
    {
        this.jButtonModificar.setEnabled(false);
        this.jButtonNuevo.setEnabled(false);
    }

    private boolean controlarBlanco()
    {
        boolean bandera  = true;
        //controlar que haya marcado un estado de animo
//        if(!this.util.controlarMarca(this.jTableEstadosAnimo))
//            bandera = false;
        //controlar que le haya asignado un nombre al perfil
        if(this.jTextFieldNombre.getText().trim().length() == 0)
            bandera = false;
        return bandera;
    }

    private void guardar()
    {
        if(this.controlarBlanco())
        {
            Perfil perfil = this.getPerfil();
            this.util.guardar(perfil);
            this.jButtonGuardar.setEnabled(false);
            this.habilitarBotones();
            this.deshabilitar();
            this.limpiarTodo();
            this.llenarTablaPerfiles();
        }
        else
            JOptionPane.showMessageDialog(null, "Hay campos vacios, verifique el nombre del perfil este cargado o que este marcado un estado de animo", "Faltan Datos", JOptionPane.ERROR_MESSAGE);
    }

    private void modificar()
    {
        if(this.controlarBlanco())
        {
            Perfil perfil = this.getPerfil();
            perfil.setCodigo(Integer.parseInt(String.valueOf(this.jTablePerfiles.getValueAt(this.jTablePerfiles.getSelectedRow(), 0))));
            this.util.modificar(perfil);
            this.jButtonGuardar.setEnabled(false);
            this.habilitarBotones();
            this.deshabilitar();
            this.limpiarTodo();
            this.llenarTablaPerfiles();
        }
        else
            JOptionPane.showMessageDialog(null, "Hay campos vacios, verifique el nombre del perfil este cargado o que este marcado un estado de animo", "Faltan Datos", JOptionPane.ERROR_MESSAGE);
    }
    
    private void borrar()
    {
        Perfil perfil = this.getPerfil();
        perfil.setCodigo(Integer.parseInt(String.valueOf(this.jTablePerfiles.getValueAt(this.jTablePerfiles.getSelectedRow(), 0))));
        this.util.borrar(perfil);
        this.limpiarTodo();
        this.llenarTablaPerfiles();
    }

    private Perfil getPerfil()
    {
        Perfil perfil = new Perfil();
        perfil.setCanales(this.canales);
        perfil.setMusica(this.temas);
        perfil.setReceta(this.recetas);
        perfil.setNombrePerfil(this.jTextFieldNombre.getText().trim().toUpperCase());
        perfil.setIntesidadLuz(this.jSliderTemperatura.getValue());
//        perfil.setEstadoAnimo(this.util.getEstadoDeAnimo(this.jTableEstadosAnimo, this.estados));
        perfil.setCarita(this.getCarita());
        return perfil;
    }

    private String getCarita()
    {
        String nombreCarita = "";
        switch(this.jComboBoxCaritas.getSelectedIndex())
        {
            case 0:
                    nombreCarita = "SinEmocion.jpg";
                    break;
            case 1:
                    nombreCarita = "Enfermo.jpg";
                    break;
            case 2:
                    nombreCarita = "Enojado.jpg";
                    break;
            case 3:
                    nombreCarita = "Feliz.jpg";
                    break;
            case 4:
                    nombreCarita = "Triste.jpg";
                    break;
        }
        return nombreCarita;
    }

    private void mostrarPerfil()
    {
        Perfil perfil = this.util.getPerfil(Integer.parseInt(String.valueOf(this.jTablePerfiles.getValueAt(this.jTablePerfiles.getSelectedRow(), 0))), this.perfiles);
        this.jTextFieldNombre.setText(perfil.getNombrePerfil());
        this.canales = perfil.getCanales();
        this.temas = perfil.getMusica();
        this.recetas = perfil.getReceta();
        this.util.cargarTablaCanales(this.jTableCanales, this.canales);
        this.util.cargarTablaRecetas(this.jTableRecetas, this.recetas);
        this.util.cargarTablaTemas(this.jTableMusica, this.temas);
//        this.util.marcarEstadoDeAnimo(this.jTableEstadosAnimo, perfil.getEstadoAnimo().getCodigo());
        this.jSliderTemperatura.setValue((int)perfil.getIntesidadLuz());
        //mostrar la carita
        if(perfil.getCarita().trim().toUpperCase().equals("SINEMOCION.JPG"))
            this.jComboBoxCaritas.setSelectedIndex(0);
        if(perfil.getCarita().trim().toUpperCase().equals("ENFERMO.JPG"))
            this.jComboBoxCaritas.setSelectedIndex(1);
        if(perfil.getCarita().trim().toUpperCase().equals("ENOJADO.JPG"))
            this.jComboBoxCaritas.setSelectedIndex(2);
        if(perfil.getCarita().trim().toUpperCase().equals("FELIZ.JPG"))
            this.jComboBoxCaritas.setSelectedIndex(3);
        if(perfil.getCarita().trim().toUpperCase().equals("TRISTE.JPG"))
            this.jComboBoxCaritas.setSelectedIndex(4);
    }
}
